<?php
namespace app\api\controller\order;

use app\api\controller\Api;
use app\common\order\model\Suborder as SuborderModel;
use app\common\order\model\SuborderBom;
use app\common\order\validate\SuborderValidate;

class Suborder extends Api {
    
    public function getList() {
        $post = $this->request->post();
        if (empty($post['order_id'])) {
            return error([], 'order_id参数异常');
        }

        $model = new SuborderModel();
        $where = [];
        $where['order_id'] = $post['order_id'];
        $rows = $model->where($where)->order('id desc')->select();
        foreach ($rows as $row) {
            $row->append(['user', 'bom']);
        }

        return success($rows, '获取数据成功');
    }

    public function create() {
        $post = $this->request->post();
        $user = $this->request->user;

        $validate = new SuborderValidate();
        if (!$validate->check($post)) {
            return error([], $validate->getError());
        }

        if (empty($post['goods'])) {
            return error([], 'goods参数异常');
        }

        // 计算总计
        $totals = 0;
        foreach ($post['goods'] as $goods) {
            foreach ($goods['spec'] as $item) {
                if ($item['give'] > 0) {
                    continue;
                } else {
                    $totals += $item['price'] * $item['amount'];
                }
            }
        }

        $totals = bcsub($totals, $post['discount'], 2);
        // var_dump($totals);
        // 比较总计
        if ($totals != $post['totals']) {
            return error([], 'totals参数异常');
        }

        // 创建订单
        $model = new SuborderModel();
        $model->startTrans();
        try {
            $model->save([
                'order_id' => $post['order_id'],
                'suborder_no' => $model->generateSuborderNo(),
                'user_id' => $user['id'],
                'totals' => $post['totals'],
                'discount' => $post['discount'],
                'status' => 1,
                'remark' => $post['remark'],
                'create_time' => time()
            ]);

            $suborderId = $model->id;

            // 创建订单BOM
            $bomModel = new SuborderBom();
            $bom = [];
            $now = time();
            foreach ($post['goods'] as $goods) {
                foreach ($goods['spec'] as $item) {
                    $bom[] = [
                        'order_id' => $post['order_id'],
                        'suborder_id' => $suborderId,
                        'product_id' => $goods['id'],
                        'title' => $goods['title'],
                        'suk' => $item['suk'],
                        'product_unit' => $goods['unit_name'],
                        'product_uniq' => $item['unique'],
                        'amount' => $item['amount'],
                        'price' => $item['price'],
                        'give' => $item['give'],
                        'create_time' => $now
                    ];
                }
            }

            $bomModel->saveAll($bom);
            $model->commit();

        } catch (\Exception $e) {
            $model->rollback();
            return error([], $e->getMessage());
        }

        return success([], '创建订单成功');
    }
    
}